package com.ztz.mynewdemo.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.ztz.mynewdemo.HideRecyclerListener;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.adapter.TodayAdapter;
import com.ztz.mynewdemo.bean.TodayResponse;
import com.ztz.mynewdemo.utils.NewsApi;

import java.util.ArrayList;
import java.util.List;

public class TodayHistoryActivcity extends BaseActivity {
    Toolbar toolbar;
    RecyclerView recyclerView;
    List<TodayResponse.ResultBean> resultBeanList=new ArrayList<>();
    TodayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_today_history_activcity);
        toolbar=(Toolbar)findViewById(R.id.toolbar_today);
        setSupportActionBar(toolbar);
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        recyclerView=(RecyclerView)findViewById(R.id.recycler_today);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter=new TodayAdapter(this,resultBeanList);
        recyclerView.setAdapter(adapter);
        ImageView iv=(ImageView)findViewById(R.id.todayIv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerView.scrollToPosition(0);
            }
        });
        recyclerView.setOnScrollListener(new HideRecyclerListener() {
            @Override
            public void Hide() {
                hideViews();
            }

            @Override
            public void Show() {
                showViews();
            }
        });
        getData();
    }

    private void hideViews() {
        toolbar.animate().translationY(-toolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2));
    }
    private void showViews(){
        toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
    }
    private void getData() {
        OkGo.<String>get(NewsApi.TODAY)
                .tag(this)
                .params("key","907298e415a8318e78b1cb4a3403c01e")
                .params("v",1.0)
                .params("month",10)
                .params("day",1)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String s=response.body();
                        Gson gson=new Gson();
                        TodayResponse todayResponse=gson.fromJson(s,TodayResponse.class);
                        try{
                            resultBeanList.addAll(todayResponse.getResult());
                        }catch (Exception e){}
                        adapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
